package com.gua.jbs.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gua.jbs.dao.IOrderDao;
import com.gua.jbs.pojo.Jbsoht;
import com.gua.jbs.pojo.Jbsoit;
import com.gua.jbs.pojo.Jbspit;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.DBConstant;
import com.gua.jbs.utility.ExceptionMessage;
import com.gua.jbs.utility.QueryConstant;
import com.gua.jbs.utility.StringUtil;

public class OrderDao extends BaseDao implements IOrderDao {

	/**
	 * Method to save the order status
	 */
	public boolean saveOrderHistory(String orderId, String prevState,
			String newState) throws ApplicationException {
		boolean isSaved = false;
		List lParamList = new ArrayList();
		int pk = 0;
		// set the parameters
		try {
			pk = getPrimaryKey("ORDER_HISTORY_ID", "JBSOHT");
			lParamList.add(0, pk);
			lParamList.add(1, orderId);
			lParamList.add(2, prevState);
			lParamList.add(3, newState);
			lParamList.add(4, DBConstant.IS_ACTIVE_YES);
			lParamList.add(5, new Timestamp(new Date().getTime()));
			lParamList.add(6, new Timestamp(new Date().getTime()));
			lParamList.add(7, null);
			lParamList.add(8, null);
			lParamList.add(9, null);
			lParamList.add(10, "Yogesh");
			lParamList.add(11, "Yogesh");
			// Get the query by passing parameters
			String query = getQuery(QueryConstant.SAVE_ORDER_STATE, lParamList);
			int resultCount = 0;
			resultCount = insert(query);
			if (resultCount != 0)
				isSaved = true;
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.ORDER_STATUS_NOT_SAVED);
		}
		return isSaved;
	}

	public int saveOrder(Jbsoit jbsoit) throws ApplicationException {
		boolean isSaved = false;
		List lParamList = new ArrayList();
		int pk = 0;
		if (jbsoit != null) {
			try {
				pk = getPrimaryKey("ORDER_ID", "JBSOIT");
				lParamList.add(0, pk);
				lParamList
						.add(1,
								jbsoit.getCustId() == ""
										|| jbsoit.getCustId() == null ? "0"
										: jbsoit.getCustId());
				lParamList
						.add(2,
								jbsoit.getProdId() == ""
										|| jbsoit.getProdId() == null ? null
										: jbsoit.getProdId());
				lParamList.add(
						3,
						jbsoit.getProdQuantity() == ""
								|| jbsoit.getProdQuantity() == null ? "0"
								: jbsoit.getProdQuantity());
				lParamList.add(4, jbsoit.getIsMainOrder());
				lParamList.add(
						5,
						jbsoit.getParentId() == ""
								|| jbsoit.getParentId() == null ? "0" : jbsoit
								.getParentId());
				lParamList.add(6, jbsoit.getPurchaseAmount());
				lParamList.add(7, jbsoit.getTotalPrice());
				lParamList.add(8, jbsoit.getPurchaseDate());
				lParamList.add(9,
						jbsoit.getIsDiscount() == ""
								|| jbsoit.getIsDiscount() == null ? "0"
								: jbsoit.getIsDiscount());
				lParamList.add(10, jbsoit.getDiscountPercentage() == ""
						|| jbsoit.getDiscountPercentage() == null ? "0.0"
						: jbsoit.getDiscountPercentage());
				lParamList.add(
						11,
						jbsoit.getCouponId() == ""
								|| jbsoit.getCouponId() == null ? "0" : jbsoit
								.getCouponId());
				lParamList.add(12, DBConstant.IS_ACTIVE_YES);
				lParamList.add(13,
						new java.sql.Date(System.currentTimeMillis()));
				lParamList.add(14,
						new java.sql.Date(System.currentTimeMillis()));
				lParamList.add(15, jbsoit.getExtrafield1());
				lParamList.add(16, jbsoit.getExtrafield2());
				lParamList.add(17, jbsoit.getExtrafield3());
				lParamList.add(18, "Piyush");
				lParamList.add(19, "Piyush");
				lParamList.add(
						20,
						jbsoit.getPaymentMode() == ""
								|| jbsoit.getPaymentMode() == null ? null
								: jbsoit.getPaymentMode());
				lParamList.add(
						21,
						jbsoit.getReferenceNo() == ""
								|| jbsoit.getReferenceNo() == null ? null
								: jbsoit.getReferenceNo());
				// Get the query by passing parameters
				String query = getQuery(QueryConstant.SAVE_ORDER, lParamList);
				int resultCount = 0;
				resultCount = insert(query);
				if (resultCount != 0)
					isSaved = true;
			} catch (SQLException sqe) {
				throw new ApplicationException(ExceptionMessage.ORDER_NOT_SAVED);
			}
		}
		return pk;
	}

	/**
	 * Private Method to update the parent order Id to an Order
	 * 
	 * @param parentId
	 * @param orderId
	 * @return
	 * @throws ApplicationException
	 */
	public boolean updateOrderForParent(String parentId, String orderId)
			throws ApplicationException {
		boolean isSaved = false;
		List lParamList = new ArrayList();
		lParamList.add(0, parentId);
		lParamList.add(1, orderId);
		// Get the query by passing parameters
		String query = getQuery(QueryConstant.UPDATE_ORDER_BY_PARENT_ID,
				lParamList);
		int resultCount = 0;
		try {
			resultCount = update(query);
			if (resultCount != 0)
				isSaved = true;
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.ORDER_NOT_UPDATED_FOR_PARENT_ID);
		}
		return isSaved;
	}

	/**
	 * Method to get all active orders of System
	 */
	public List<Object[]> getAllActiveOrders() throws ApplicationException {
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.IS_ACTIVE_YES);
		lParamList.add(1, DBConstant.ORDER_DEFAULT_PARENT_ID);
		// Get the query by passing parameters
		String query = getQuery(QueryConstant.GET_ALL_ACTIVE_ORDERS, lParamList);
		List<Object[]> orderList = null;

		try {
			ResultSet rs = getResults(query);
			orderList = new ArrayList<Object[]>();
			while (rs.next()) {
				Object[] valueArray = new Object[4];
				valueArray[0] = rs.getInt("ORDER_ID");
				valueArray[1] = rs.getString("CUSTOMER");
				valueArray[2] = rs.getBigDecimal("AMOUNT");
				valueArray[3] = rs.getDate("ORDER_DATE");
				orderList.add(valueArray);
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.ORDER_LIST_UNABLE_TO_FETCH);
		}
		return orderList;
	}
}
